#! /usr/bin/env python
# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui
import sys, os, sqlite3, todo
from elixir import *
from ventanaAnulacionUI import Ui_Form

class FormAnulacion(QtGui.QMainWindow):
    def __init__(self,parent=None):
        QtGui.QWidget.__init__(self,parent)
        self.ui = Ui_Form()
        self.center()
        self.ui.setupUi(self)

        self.ui.treeWidget.resizeColumnToContents(0)
        self.ui.treeWidget.resizeColumnToContents(1)
        self.ui.treeWidget.resizeColumnToContents(2)
        self.ui.treeWidget.resizeColumnToContents(3)
        self.ui.treeWidget.resizeColumnToContents(4)
        self.cargaVentas()

    def center(self):
        qr = self.frameGeometry()
        cp = QtGui.QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def cargaVentas(self):
        self.ui.treeWidget.clear()
        for venta in todo.Venta.query.all():
            if venta.nula == True:
                nula = "Nula"

            else:
                nula = "No nula"

            item = QtGui.QTreeWidgetItem([str(venta.id), str(venta.fecha), venta.forma_pago, str(venta.total), str(venta.vendedor), nula])
            item.setTextAlignment(0, 2)
            item.setTextAlignment(3, 2)
            self.ui.treeWidget.addTopLevelItem(item)

    def anular(self):
        item = self.ui.treeWidget.currentItem()
        venta = todo.Venta.get(int(item.text(0)))
        if venta.nula == True:
            venta.nula = False

        else:
            venta.nula = True
        
        session.commit()
        self.cargaVentas()

if __name__ == '__main__':
    todo.initDB()
    app = QtGui.QApplication(sys.argv)
    myapp = FormNuevaVenta()
    myapp.show()
    sys.exit(app.exec_())